package user_private_key

import (
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/operation_log"
	userModel "github.com/xinliangnote/go-gin-api/internal/repository/mongodb/user"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/user_private_key"
	"github.com/xinliangnote/go-gin-api/pkg/errors"
	"time"
)

type UserPrivateKeyData struct {
	Id                 string  `bson:"_id,omitempty" json:"id"`
	UserId             string  `bson:"userId" json:"userId"`
	MainCoinType       int64   `bson:"mainCoinType" json:"mainCoinType"`       // udun的id
	FeeAddress         string  `bson:"feeAddress" json:"feeAddress"`           // 手续费地址-钱包地址
	CollectAddress     string  `bson:"collectAddress" json:"collectAddress"`   // 归集地址
	WithdrawAddress    string  `bson:"withdrawAddress" json:"withdrawAddress"` // 提币地址-钱包地址
	ChainName          string  `bson:"chainName" json:"chainName"`
	MinAmount          float64 `bson:"minAmount" json:"minAmount"`                   // 最小归集金额
	FeePrivateKey      string  `bson:"feePrivateKey" json:"feePrivateKey"`           //手续费私钥加密后的字符串
	WithdrawPrivateKey string  `bson:"withdrawPrivateKey" json:"withdrawPrivateKey"` //提现私钥加密后的字符串
	FeeBusinessId      string  `bson:"feeBusinessId" json:"-"`                       //业务id
	WithdrawBusinessId string  `bson:"withdrawBusinessId" json:"-"`                  //业务id
}

type QueryData struct {
	UserId         string
	PageIndex      uint
	PageSize       uint
	ChainName      string
	FeeAddress     string
	CollectAddress string
}

func (s *service) Create(ctx core.Context, keyData *UserPrivateKeyData) (err error) {
	model := user_private_key.NewModel()

	if keyData.FeeAddress[0:2] == "0x" {
		model.FeeAddress = keyData.FeeAddress[2:]
	} else {
		model.FeeAddress = keyData.FeeAddress
	}
	//if keyData.WithdrawAddress[0:2] == "0x" {
	//	model.WithdrawAddress = keyData.WithdrawAddress[2:]
	//} else {
	//	model.WithdrawAddress = keyData.WithdrawAddress
	//}

	_, err = user_private_key.PrivateToAddress(model.FeeAddress)
	if err != nil {
		return err
	}
	//_, err = user_private_key.PrivateToAddress(model.WithdrawAddress)
	//if err != nil {
	//	return err
	//}

	model.CollectAddress = keyData.CollectAddress
	model.UserId = keyData.UserId
	model.MainCoinType = keyData.MainCoinType
	model.ChainName = keyData.ChainName

	flag, err := model.GetInfoCount(s.db.GetDb())
	if err != nil {
		return err
	}
	if !flag {
		return errors.New("请勿重复添加!")
	}
	id, err := model.Create(s.db.GetDb())

	if err == nil {
		userInfo := ctx.GetInfo("userInfo").(*userModel.User)
		logModel := operation_log.NewModel()
		logModel.FTable = "user_private_key"
		logModel.Type = "add"
		logModel.FId = id
		logModel.IpAddr = ctx.GetClientIP()
		logModel.AdminName = userInfo.Username
		logModel.Content = "新增钱包:公链名称[" + keyData.ChainName + "],归集地址:[" + keyData.CollectAddress + "]"
		logModel.Timestamp = time.Now().Unix()
		logModel.Create(s.db.GetDb())
	}
	return err
}

func (s *service) List(ctx core.Context, data *QueryData) (keyList []user_private_key.UserPrivateKey, err error) {
	model := user_private_key.NewModel()
	model.UserId = data.UserId
	model.ChainName = data.ChainName
	model.CollectAddress = data.CollectAddress
	model.FeeAddress = data.FeeAddress
	list, err := model.List(s.db.GetDb(), data.PageIndex, data.PageSize)
	return list, err
}

func (s *service) Update(ctx core.Context, keyData *UserPrivateKeyData) (err error) {
	model := user_private_key.NewModel()
	model.FeeAddress = keyData.FeeAddress
	model.CollectAddress = keyData.CollectAddress
	//model.WithdrawAddress = keyData.WithdrawAddress
	model.Id = keyData.Id
	err = model.Update(s.db.GetDb())

	if err != nil {
		logModel := operation_log.NewModel()
		logModel.FTable = "user_private_key"
		logModel.Type = "edit"
		logModel.FId = keyData.Id
		logModel.IpAddr = ctx.GetClientIP()
		logModel.AdminName = ctx.SessionUserInfo().UserName
		logModel.Content = "更新钱包:id[" + keyData.Id + "],公链名称[" + keyData.ChainName + "],归集地址:[" + keyData.CollectAddress + "]"
		logModel.Timestamp = time.Now().Unix()
		logModel.Create(s.db.GetDb())

	}
	return err
}

func (s *service) Count(ctx core.Context, userId string) (count int64, err error) {
	model := user_private_key.NewModel()
	model.UserId = userId
	num, err := model.Count(s.db.GetDb())
	return num, err
}
